package com.wkh.project.scoring;

import com.wkh.project.aop.ScoringStrategyConfig;
import com.wkh.project.common.ErrorCode;
import com.wkh.project.exception.BusinessException;
import com.wkh.project.exception.ThrowUtils;
import com.wkh.project.model.entity.App;
import com.wkh.project.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wangkehua
 */
@Service
public class ScoringStrategyExecutor {
    //策略列表
    @Resource
    private List<ScoringStrategy> scoringStrategyList;

    /**
     * 执行评分
     * @param choice
     * @param app
     * @return
     * @throws Exception
     */
    public UserAnswer doScore(List<String> choice , App app) throws Exception{
        Integer appType = app.getAppType();
        Integer scoringStrategy = app.getScoringStrategy();
        ThrowUtils.throwIf(appType == null || scoringStrategy == null, ErrorCode.PARAMS_ERROR,"应用配置有误，未找到匹配策略");
        //根据注解获取策略
        for (ScoringStrategy strategy : scoringStrategyList) {
            if (strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)) {
                ScoringStrategyConfig scoringStrategyConfig = strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if (scoringStrategyConfig.appType() == appType && scoringStrategyConfig.scoringStrategy() == scoringStrategy) {
                    return strategy.doScore(choice, app);
                }
            }
        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR,"应用配置有误，未找到匹配策略");
    }
}
